package com.gl.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by DELL on 2017/12/5.
 */
@RestController
@RequestMapping("/hello")
public class TestController {

    private final Logger logger = Logger.getLogger(TestController.class);

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/date")
    public String getDate(){

        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello, host:" + instance.getHost() + ", port:" + instance.getPort() + ",service_id:" + instance.getServiceId() );

/*        List<ServiceInstance> instances = client.getInstances("hello-service1");
        for (int i = 0; i < instances.size(); i++) {
            logger.info("/Hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
        }*/
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return "Hello,Service1,My port is " + instance.getPort() + ",当前时间:"+ sf.format(d);
    }

}
